import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   如果设置为 true，项目将不会显示在侧边栏中（默认为 false）
 * alwaysShow: true               如果设置为 true，将始终显示根菜单
 *                                如果不设置 alwaysShow，当 item 有多个子路由时，
 *                                它将变成嵌套模式，否则不显示根菜单
 * redirect: noRedirect           如果设置 noRedirect 将不会在面包屑中重定向
 * name:'router-name'             该名称由 <keep-alive> 使用（必须设置！！！）
 * meta : {
    roles: ['admin','editor']    控制页面角色（可以设置多个角色）
    title: 'title'               名称显示在侧边栏和面包屑中（推荐设置）
    icon: 'svg-name'/'el-icon-x' 图标显示在侧边栏中
    breadcrumb: false            如果设置为 false，该项目将隐藏在面包屑中（默认为 true）
    activeMenu: '/example/list'  如果设置路径，侧边栏将突出显示您设置的路径
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
//  登录页面
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  // 工作台
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: '/dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '工作台', icon: 'dashboard' }
    }]
  },
  // 支付
  {
    path: '/payment',
    component: Layout,
    redirect: 'noRedirect',
    name: 'Payment',
    meta: { title: '支付', icon: 'el-icon-s-order' },
    children: [
      {
        path: 'memberPayment',
        name: 'MemberPayment',
        component: () => import('@/views/payment/memberPayment/index'),
        meta: { title: '支付', icon: 'el-icon-s-claim' }
      }
    ]
  },
  // 充值
  {
    path: '/recharge',
    component: Layout,
    children: [{
      path: '/recharge',
      name: 'Recharge',
      component: () => import('@/views/recharge/index'),
      meta: { title: '充值', icon: 'el-icon-money' }
    }]
  },
  // 产品录入
  {
    path: '/goods',
    component: Layout,
    children: [{
      path: '/goods',
      name: 'Goods',
      component: () => import('@/views/goods/index'),
      meta: { title: '产品', icon: 'el-icon-s-goods' }
    }]
  },
  // 充值
  {
    path: '/registerMember',
    component: Layout,
    children: [{
      path: '/registerMember',
      name: 'RegisterMember',
      component: () => import('@/views/registerMember/index'),
      meta: { title: '注册会员', icon: 'el-icon-s-custom' }
    }]
  },
  // 我的设置
  {
    hidden: true,
    path: '/myUpset',
    component: Layout,
    children: [{
      path: '/myUpset',
      name: 'MyUpset',
      component: () => import('@/views/myUpset/index'),
      meta: { title: '设置' }
    }]
  },
  // TODO 统计页面
  // 404 页面必须放在最后！！！
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
